﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sahit.iSoilHotel.Infrastructure.Web.DependencyManagement;
using Autofac;
using Autofac.Integration.Mvc;
using System.Web;
using Autofac.Core;
using System.Reflection;
using Autofac.Builder;
using Sahit.iSoilHotel.Infrastructure.Web.Configuration;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern;
using Sahit.iSoilHotel.Infrastructure.Web.Helper;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Authentication;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Record;
using Sahit.iSoilHotel.Infrastructure.Crosscutting.Adapter;
using Sahit.iSoilHotel.Infrastructure.Crosscutting.NetFramework.Adapter;
using Sahit.iSoilHotel.Infrastructure.Web.Caching;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Room;
using Sahit.iSoilHotel.Infrastructure.Web.Events;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Price;

namespace Sahit.iSoilHotel.Portal.Framework
{
    public class DependencyRegistrarx : IDependencyRegistrar
    {
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.Register(c =>
                new HttpContextWrapper(HttpContext.Current) as HttpContextBase)
                .As<HttpContextBase>()
                .InstancePerHttpRequest();

            builder.Register(c => c.Resolve<HttpContextBase>().Request)
                .As<HttpRequestBase>()
                .InstancePerHttpRequest();

            builder.Register(c => c.Resolve<HttpContextBase>().Response)
                .As<HttpResponseBase>()
                .InstancePerHttpRequest();

            builder.Register(c => c.Resolve<HttpContextBase>().Server)
                .As<HttpServerUtilityBase>()
                .InstancePerHttpRequest();

            builder.Register(c => c.Resolve<HttpContextBase>().Session)
                .As<HttpSessionStateBase>()
                .InstancePerHttpRequest();


            builder.RegisterControllers(typeFinder.GetAssemblies().ToArray());

            builder.RegisterType<WebHelper>().As<IWebHelper>().InstancePerHttpRequest();

            builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().Named<ICacheManager>("iSoil_Cache_Static").SingleInstance();

            builder.RegisterType<TavernAuthenticationServiceProxy>().As<ITavernAuthenticationServiceProxy>().InstancePerHttpRequest();
            builder.RegisterType<TavernRecordServiceProxy>().As<ITavernRecordServiceProxy>().InstancePerHttpRequest();
            builder.RegisterType<TavernRoomServiceProxy>().As<ITavernRoomServiceProxy>().InstancePerHttpRequest();
            builder.RegisterType<TavernPriceServiceProxy>().As<ITavernPriceServiceProxy>().InstancePerHttpRequest();

            //Register event consumers
            var consumers = typeFinder.FindClassesOfType(typeof(IConsumer<>)).ToList();
            foreach (var consumer in consumers)
            {
                builder.RegisterType(consumer)
                    .As(consumer.FindInterfaces((type, criteria) =>
                    {
                        var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition());
                        return isMatch;
                    }, typeof(IConsumer<>)))
                    .InstancePerHttpRequest();
            }

            builder.RegisterType<EventPublisher>().As<IEventPublisher>().SingleInstance();
            builder.RegisterType<SubscriptionService>().As<ISubscriptionService>().SingleInstance();
        }

        public int Order
        {
            get { return 0; }
        }
    }
}
